public class NationalOpsCaseStatusEditController{

    private static final string ioRecordTypeName = 'National Ops - Insertion Order';
    
    public NationalOpsCaseStatusEditController (ApexPages.StandardController controller){
        this.controller = controller;
                
        Case c = (Case)controller.getRecord();
        //ID caseID = ApexPages.currentPage().getParameters().get('id');
        ID caseID = c.ID;
        caseObject = [Select Id, RecordType.Name, Status, Released_Live__c, Future_Start_Date__c, NATO_QC_Level__c, Nato_Rep_SS__c, Order_Entry__c, Ad_Production__c, Quality_Control__c, Nato_Rep_QC2__c, Billing__c from Case where id = :caseID];
    }      
    
    public List<SelectOption> getNewStatusOptions(){
      
        LoadStatusOptions();     
        return options;
    }
 
     private void LoadStatusOptions()
     {
         boolean billingStatesAllows = caseObject.RecordType.Name == ioRecordTypeName;
         
         if(options == null){
             options = new List<SelectOption>();
         
            Set<string> states = NatoCaseStateTable.GetPossibleStates(caseObject.Status, caseObject.Released_Live__c, billingStatesAllows, caseObject.Future_Start_Date__c, caseObject.NATO_QC_Level__c,
                caseObject.Nato_Rep_SS__c, caseObject.Order_Entry__c, caseObject.Ad_Production__c, caseObject.Quality_Control__c, caseObject.Nato_Rep_QC2__c, caseObject.Billing__c);
            
            for(string state : states){
                options.add(new SelectOption(state, state));
            }
         }
     } 
     
    public boolean getShowResponseField(){
        return true; //NatoCaseStateTable.IsResponse(selectedStatus);
    }
    
    public integer getlistSize(){
        LoadStatusOptions();
        return Math.Min(options.size(), 5); // Maximum possible list size is 5
    }
     
    public PageReference save(){
        PageReference pageRef = null;
        CaseComment comment = null;
        SavePoint sp = null;
            
        if(getShowResponseField() && CaseResponse != null && CaseResponse.length() > 0){
            comment = new CaseComment();
            comment.CommentBody = CaseResponse;
            comment.IsPublished = false;
            comment.ParentId = caseObject.ID;
        }
        
        Case c = new Case(ID = caseObject.ID);
        c.Status = selectedStatus;
        
        try{
            // If we're adding comments we'll require a transaction for possible rollbacks
            boolean isTransactionRequired = (null != comment);
            
            if(isTransactionRequired)
                sp = database.setSavepoint();
                
            if(null != comment)
                insert comment;
            
            update c;
            pageRef = GetReloadParentPageReference();
        }
        catch(DMLException ex){
            if(null != sp)
                database.rollback(sp);
                
            ApexPages.addMessages(ex);            
        }
        
        return pageRef;
    }   
    
    private PageReference GetReloadParentPageReference(){       
        Case c = new Case(ID = caseObject.ID);
        PageReference parentRef= new ApexPages.StandardController(c).view();
      
        string baseUrl = ApexPages.currentPage().getHeaders().get('Host');
        string fullUrl = 'https://' + baseUrl + parentRef.getUrl();
      
        PageReference ref = Page.GenericParentRedirectPage;
        ref.getParameters().put('url', fullUrl);
        ref.setRedirect(true);
        return ref;        
    }
    
        
    private List<SelectOption> options = null;
    
    // reference for the standard controller
    private ApexPages.StandardController controller {get; set;}
    public Case caseObject{ get; set; }
    public string SelectedStatus{ get; set; }
    public string CaseResponse {get; set;}
     
}